/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Cyn
 */

@Entity
@XmlRootElement
@NamedQueries({
    @NamedQuery(name="CompteBancaire.findAll", query="select cb from CompteBancaire cb"),
    @NamedQuery(name="CompteBancaire.findById", query="select cb from CompteBancaire cb where cb.id = :compteid"),
    @NamedQuery(name="CompteBancaire.findByProprio", query="select cb from CompteBancaire cb where cb.proprietaire = :proprio"),
    @NamedQuery(name="CompteBancaire.findAllWithout", query="select cb from CompteBancaire cb where cb.proprietaire <> :id"),
    @NamedQuery(name="CompteBancaire.search", query="select cb from CompteBancaire cb where cb.proprietaire LIKE :query"),
    @NamedQuery(name = "CompteBancaire.nbComptes", query = "SELECT COUNT(c) FROM CompteBancaire c")
})
public class CompteBancaire implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String proprietaire;
    private int solde;
    @ManyToOne(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private TypeCompte type;
    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    private List<OperationBancaire> operations = new ArrayList();

    
    public CompteBancaire() {
    }

    public CompteBancaire(int solde, TypeCompte type) {
        this.solde = solde;
        this.type = type;
    }
    
    public List<OperationBancaire> getOperations() {
        return operations;
    }

    public void setOperations(List<OperationBancaire> operations) {
        this.operations = operations;
    }
    public void addOperationBancaire(OperationBancaire o){
        operations.add(o);
    }

    
    public CompteBancaire(String proprietaire, int compte) {
        this.proprietaire = proprietaire;
        this.solde = compte;
    }

    public int getSolde() {
        return solde;
    }

    public void setSolde(int solde) {
        this.solde = solde;
    }

    public String getProprietaire() {
        return proprietaire;
    }

    public void setProprietaire(String proprietaire) {
        this.proprietaire = proprietaire;
    }
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public TypeCompte getType() {
        return type;
    }

    public void setType(TypeCompte type) {
        this.type = type;
    }
    
    public boolean retirerArgent(int somme){
        if(solde<somme){
            return false;
        }else{
            this.solde = (this.solde-somme);
            return true;
        }
    }
    
    public void ajouterArgent(int somme){
        this.solde = (this.solde+somme);
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof CompteBancaire)) {
            return false;
        }
        CompteBancaire other = (CompteBancaire) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.CompteBancaire[ id=" + id + " ]";
    }
    
}
